package com.xingxing.Dao;

import com.xingxing.Bean.Comment;
import com.xingxing.Bean.File;
import com.xingxing.Util.C3P0Util;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

public class CommentDao {
    public static boolean insert(Comment comment) {
        int num = 0;
        Connection conn = null;
        try {
            conn = C3P0Util.getConnection();
            QueryRunner runner = new QueryRunner();
            String sql = "insert into Comment values(?,?,?,?,?);";
            num = runner.update(conn, sql,new Object[]{
                    comment.getCommentID(),
                    comment.getCommentText(),
                    comment.getVideoID(),
                    comment.getUserAccount(),
                    comment.getCommentDate()
            });
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (num > 0) return true;
        return false;
    }

    public static ArrayList<Comment> findAll() {
        ArrayList list = null;
        try {
            QueryRunner runner = new QueryRunner();
            String sql = "select * from Comment";
            list = (ArrayList) runner.query(C3P0Util.getConnection(), sql, new BeanListHandler(Comment.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static ArrayList<Comment> findVideoComment(String videoID) {
        ArrayList list = null;
        try {
            QueryRunner runner = new QueryRunner();
            String sql = "select * from Comment where videoID=?";
            list = (ArrayList) runner.query(C3P0Util.getConnection(), sql, new BeanListHandler(Comment.class),new Object[]{videoID});
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static Comment find(String commentID) {
        Comment comment = null;
        try {
            QueryRunner runner = new QueryRunner();
            String sql = "select * from Comment where commentID = ?";
            comment = (Comment) runner.query(C3P0Util.getConnection(), sql, new BeanHandler(Comment.class), new Object[]{commentID});
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return comment;
    }

    public static boolean update(Comment comment){
        if (delete(comment.getCommentID()) && insert(comment)) return true;
        else return false;
    }

    public static boolean delete(String commentID) {
        try {
            QueryRunner runner = new QueryRunner(C3P0Util.getDataSource());
            String sql = "delete from Comment where commentID = ?";
            if (runner.update(sql, new Object[]{commentID}) > 0) return true;
            return false;
        }catch (SQLException e){
            e.printStackTrace();
        }
        return false;
    }
}
